// basic/components/gcs-component/news/news-detail.js
const app = getApp();
Component({
    options: {
        multipleSlots: true,
        addGlobalClass: true
    },
    /**
     * 组件的属性列表
     */
    properties: {
        detailData: {
            type: Object,
            default: []
        },
        pagepoint: {
            type: String,
            default: ""
        },
        operation: {
            type: Boolean,
            default: true
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        itemWidth: 0,
        detailData2: {},
        bThumbsUp: true, //能否点赞
        aThumbsUpApi: [], //0-点赞 1-取消点赞
        opObj: {}
    },
    observers: {
        'detailData': function (detailData) {
            this.formatData(detailData);
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        formatData: function (detailData) {
            var _this = this;
            //数据格式
            detailData.iTime = wx.$to_encodeTime(detailData.iTime, "Y-M-D h:m:s");
            detailData.sContent = wx.$to_decodeBase64(detailData.sContent);
            // detailData.sContent = detailData.sContent.replace(/\/street\/files\/ueditor/g, 'https://yg.wfyizhan.com/street/files/ueditor');
            // detailData.sContent = detailData.sContent.replace(/\/street\/resources\/upload/g, streeturl);
            // detailData.sContent = detailData.sContent.replace(/\/streetv2\/resources\/upload/g, streeturl);

            this.setData({
                detailData2: detailData
            })
        },
        thumbsUp: function (e) {
            var _this = this;
            if (_this.data.bThumbsUp) {
                var detailData = this.data.detailData2;
                var apiname = _this.data.aThumbsUpApi[1]; //取消点赞
                if (!detailData['iZan']) {
                    apiname = _this.data.aThumbsUpApi[0]; //点赞的api
                }
                var tpObj = {};
                if (app.globalData.api_common_news[_this.data.pagepoint]['sThumbsUpPost'] != "") {
                    tpObj[app.globalData.api_common_news[_this.data.pagepoint]['sThumbsUpPost']] = detailData['iID'];
                } else {
                    tpObj['iID'] = detailData['iID'];
                }
                wx.$requestApi(apiname, tpObj, function (res) {
                    if (res.code == 0) { //更新页面数据
                        if (detailData['iZan'] == 1) { //已点赞 取消点赞
                            if (detailData['iNumzan'] > 0) {
                                detailData['iNumzan']--;
                            }
                        } else { //点赞
                            detailData['iNumzan']++;
                        }
                        detailData['iZan'] = !detailData['iZan']; //改变点赞状态
                        _this.setData({
                            detailData2: detailData
                        })
                    } else {
                        wx.$gcs_showToast(res.msg);
                    }
                })
            } else {
                wx.$gcs_showToast("功能未开放");
            }
        },
        commentPage: function (e) {
            wx.navigateTo({
                url: 'comment?id=' + e.currentTarget.dataset.id + "&pagepoint=" + this.data.pagepoint
            })
        },
    },
    //在组件在视图层布局完成后执行
    ready() {
        // this.formatData();
        var _this = this;
        var pagepoint = this.data.pagepoint;
        var api_common_news = app.globalData.api_common_news;
        if (api_common_news.hasOwnProperty(pagepoint)) {
            var opObj = {};
            for (var i = 0; i < api_common_news[pagepoint]['sOperation'].length; i++) {
                opObj[api_common_news[pagepoint]['sOperation'][i]] = true;
            }
            _this.setData({
                opObj: opObj
            })
            //gcs-nd-handle 长度
            var itemWidth = 100,
                itemLen = 0;
            itemLen = _this.data.opObj.can_commit && _this.data.opObj.can_commit != undefined ? itemLen + 1 : itemLen;
            itemLen = _this.data.opObj.can_thumbs_up && _this.data.opObj.can_thumbs_up != undefined ? itemLen + 1 : itemLen;
            itemLen = _this.data.opObj.can_share && _this.data.opObj.can_share != undefined ? itemLen + 1 : itemLen;

            this.setData({
                itemWidth: (itemWidth / itemLen).toFixed(2)
            })
            //检测是否存在点赞接口
            if (api_common_news[pagepoint]['sThumbsUpApi'].length >= 2 && api_common_news[pagepoint]['sThumbsUpApi'] != undefined) {
                _this.setData({
                    aThumbsUpApi: api_common_news[pagepoint]['sThumbsUpApi'],
                    bThumbsUp: true
                })
            }
        }
    },
})